#include "button_edit.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QComboBox>
#include <QStackedWidget>
#include <qradiobutton.h>

#include "../../../HCINDI.h"

ButtonEdit::ButtonEdit(Button* butt)
        :QDialog(0) {
    setModal(true);
    //build the dialog!
    button = butt;
    setWindowTitle("Set " + button->getName());
    setWindowIcon(QPixmap(ICON24));

    QStackedWidget *sw = new QStackedWidget();
    
    QVBoxLayout* vv = new QVBoxLayout(this);
    vv->setMargin(5);
    vv->setSpacing(5);

    QHBoxLayout *hr = new QHBoxLayout();
    QRadioButton *qrx = new QRadioButton("X Event");
    QRadioButton *qri = new QRadioButton("INDI Event");
    BGType = new QButtonGroup();
    hr->addWidget(qrx);
    hr->addWidget(qri);
    vv->addLayout(hr);
    BGType->addButton(qrx, 0);
    BGType->addButton(qri, 1);
    BGType->setExclusive(true);
    connect(BGType, SIGNAL(buttonClicked(int)), sw, SLOT(setCurrentIndex(int)));
    
    QGroupBox *gx = new QGroupBox();
    gx->setTitle("X Event");
    gx->setFlat(true);
    QVBoxLayout* v = new QVBoxLayout(gx);
    v->setMargin(5);
    v->setSpacing(5);   
    BKKey = new KeyButton( button->getName(), button->useMouse?button->mousecode:button->keycode, this, true, button->useMouse);
    v->addWidget(BKKey);

    QHBoxLayout* h = new QHBoxLayout();
    CSticky = new QCheckBox("Sticky", this);
    CSticky->setChecked(button->sticky);
    h->addWidget(CSticky);
    CRapid = new QCheckBox("Rapid Fire", this);
    CRapid->setChecked(button->rapidfire);
    h->addWidget(CRapid);
    v->addLayout(h);
    sw->addWidget(gx);
    
    QGroupBox *gi = new QGroupBox();
    gi->setTitle("INDI Event");    
    v = new QVBoxLayout(gi);
    v->setMargin(5);
    v->setSpacing(5); 
    comboindievents = new QComboBox();
    int i=0;
    comboindievents->addItem("[NONE]", "");
    comboindievents->setCurrentIndex(0);
    QHash<QString, PadButtonEvent *> pbv = hApp->getPad()->getPadButtonEvents();
    QHashIterator<QString, PadButtonEvent *> pbit(pbv);
    while (pbit.hasNext()) {
      pbit.next();
      i+=1;
      comboindievents->addItem(pbit.value()->getLabel(), pbit.key());
      if (button->indiEvent== pbit.key()) comboindievents->setCurrentIndex(i);
      //comboindievents->addItems(QStringList()<<"[NO EVENT]" <<"Slew North" << "Slew South" << "Slew East" << "Slew West" << "Sync");
    }
    v->addWidget(comboindievents);
    sw->addWidget(gi);
    
    if (button->isIndi) {
      sw->setCurrentIndex(1); 
      qri->setChecked(true);
    }
    else {
      sw->setCurrentIndex(0);
      qrx->setChecked(true);
    }
    vv->addWidget(sw);
    
    h = new QHBoxLayout();
    BOkay = new QPushButton("Okay", this);
    connect(BOkay, SIGNAL( clicked() ), this, SLOT( accept()));
    h->addWidget(BOkay);
    BCancel = new QPushButton("Cancel", this);
    connect(BCancel, SIGNAL( clicked() ), this, SLOT( reject()));
    h->addWidget(BCancel);
    vv->addLayout(h);
}

void ButtonEdit::show() {
    QDialog::show();
    setFixedSize(size());
}

void ButtonEdit::accept() {
//if the rapidfire status has changed, either request a timer or turn it down.
    /*if (button->rapidfire) {
    	if (!CRapid->isChecked()) tossTimer(button);
    }
    else {
    	if (CRapid->isChecked()) takeTimer(button);
    }*/
    if (BGType->checkedId() == 0) {
      button->isIndi = false;
      button->rapidfire = CRapid->isChecked();
      button->sticky = (CSticky->isChecked());
      //if the user chose a mouse button...
      if (BKKey->choseMouse()) {
        button->useMouse = true;
        button->mousecode = BKKey->getValue();
      }
      else {
        button->useMouse = false;
        button->keycode = BKKey->getValue();
      }
    } else {
     button->isIndi = true;
     button->indiEvent = comboindievents->itemData(comboindievents->currentIndex()).toString();
    }

    QDialog::accept();
}
